package com.nuc.ENetcom.action;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nuc.ENetcom.entity.Student;
import com.nuc.ENetcom.service.RoleService;
import com.nuc.ENetcom.service.StudentService;
import com.nuc.ENetcom.utils.PageUtil;

@Controller
public class StudentAction {
	@Autowired
	private StudentService service;
	
	@RequestMapping("/getPage.action")
	public @ResponseBody  PageUtil<?> test2( int pageno, int pagesize) throws Exception{
         PageUtil<?> page=service.getPage1(pageno, pagesize);
		 return page; 
	}
	@RequestMapping("/lock.action")
	public void lock(HttpSession session){
		Student pic = (Student) session.getAttribute("Student");
		pic.setLock(1);
		session.setAttribute("Student", pic);
		try {
			System.out.println(pic.getSid());
			service.setLock(pic);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("/saveStudent.action")
	public @ResponseBody String save(Student	student){
		
		Student nStu=null;
		try {
			System.out.println(student.getSid()+"==================="+student.getLock());
			 nStu=service.get(student.getSid());
			 System.out.println(nStu);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		nStu.setBankcode(student.getBankcode());
		nStu.setLock(student.getLock());
		nStu.setLoss(student.getLoss());
		nStu.setMoney(student.getMoney());
		nStu.setSname(student.getSname());
		
		int i = -1;
		try {
			i=service.save(nStu);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}if (i<0) {
			return "error";
		}else{
			return "success";
		}
	}
	@RequestMapping("/chongzhi.action")
	public @ResponseBody String chongzhi(float money,HttpSession session){
		Student pic = (Student) session.getAttribute("Student");
		
		double money1=pic.getMoney()+money;
		try {
			service.changeMoney(pic,money1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success";
		
	}
	@RequestMapping("/loss.action")
	public @ResponseBody String loss(String id,String psd,HttpSession session){
		Student pic = (Student) session.getAttribute("Student");
		System.out.println(pic);
		if (id!=null&&psd!=null) {
			if (pic.getSid().equals(id)&&pic.getSpsd().equals(psd)) {
				pic.setLoss(1);
				try {
					service.save(pic);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return "success";
			}else{
				return "error";
			}
		}else{
			return "error";
		}
		
	}
	
	@RequestMapping("/getUser.action")
	public @ResponseBody Student get(String s){

		Student student=null;
		try {
			System.out.println(	s);
			 student = service.get(s);
			 System.out.println(student);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student;
	}
	@RequestMapping("/yue.action")
	public @ResponseBody Double yue(HttpSession session){
		Student student= (Student) session.getAttribute("Student");
		Student student2=null;
		try {
			student2=service.get(student.getSid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student2.getMoney();
	}
	@RequestMapping("/changepsd.action")
	public @ResponseBody String changepsd(String oldpsd,String newpsd,HttpSession session){
		Student student= (Student) session.getAttribute("Student");
		String opsd=student.getSpsd();
		if (opsd.equals(oldpsd)) {
			student.setSpsd(newpsd);
			try {
				service.save(student);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "success";
		}else{
			return "error";
		}
	}
	@RequestMapping("/bindbankcode.action")
	public @ResponseBody String bindbankcode(String bankid,String bankpsd,HttpSession session){
		Student student= (Student) session.getAttribute("Student");
		System.out.println(bankpsd);
		System.out.println(student.getSpsd());
		if (student.getSpsd().equals(bankpsd)) {
			if (student.getBankcode()==null) {
				student.setBankcode(bankid);
				try {
					service.save(student);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return "systerror";
				}
				return "success";
			}else{
				return "exist";
			}
		}else{
			return "error";
		}
	}
	@RequestMapping("/newStudent.action")
	public @ResponseBody String newStudent(Student stu){
		int i=-1;
		try {
			 i=service.save(stu);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (i<0) {
			return "error";
		}
		return "success";
	}
	@RequestMapping("/daleteUser.action")
	public @ResponseBody String daleteUser(String s){
		int i=-1;
		try {
			i=service.delete(s);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (i<0) {
			return "error";
		}
		return "success";
	}
	@RequestMapping("/buzhu.action")
	public @ResponseBody String buzhu(float money){
	System.out.println(money);
		try {
			service.buzhu(money);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "success";
	}
	@RequestMapping("/showbuzhu.action")
	public @ResponseBody double showbuzhu(HttpSession session){
		Student student= (Student) session.getAttribute("Student");
		
		try {
			student=service.get(student.getSid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student.getBuzhu();
	}
	
	@RequestMapping("/getbuzhu.action")
	public @ResponseBody void getbuzhu(HttpSession session){
		Student student= (Student) session.getAttribute("Student");
		double money=student.getBuzhu();
		chongzhi((float) money,session);
		student.setBuzhu(0);
		try {
			service.save(student);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				
	}

	@RequestMapping("/getAll.action")
	public @ResponseBody List<Student>  getAll(){
		List<Student> list= null;
		try {
			list = service.getAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				return list;
	}
	@RequestMapping("/xiaofei.action")
	public @ResponseBody void xiaofei(double money,HttpSession session){
		Student pic = (Student) session.getAttribute("Student");
		
		double money1=pic.getMoney()-money;
		try {
			service.changeMoney(pic,money1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//这个问题解决不了了。。
	@RequestMapping("/raise.action")
	public @ResponseBody void raise(float money){
		try {
			service.raise(money);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}